/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method.DataStaticGetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method.DataStaticSetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method.SetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.SimpleUdtDataValuePropGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;
import org.eclipse.jdt.core.dom.TypeLiteral;

public class AccessorDataSetValueGenerator extends ClassMethodGenerator
{
	private static final String MethodName = "innerSetValue";
	private static final String param1 = "propName";
	private static final String param2 = "value";
	private final String nodeCode;
	private final EntityGeneratorContext ctx;

	public AccessorDataSetValueGenerator(EntityGeneratorContext ctx,String nodeCode) {

		this.ctx = ctx;
		this.nodeCode = nodeCode;
	}
	@Override
	protected String getMethodName()
	{
		return MethodName;
	}
	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(void.class.getTypeName());
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}
	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(ModifierKeyword.PROTECTED_KEYWORD);
		return list;
	}
	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		ArrayList<ParameterInfo> list=new ArrayList<ParameterInfo>();
		ParameterInfo parameterInfo=new ParameterInfo();
		parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
		parameterInfo.setParamName(param1);
		list.add(parameterInfo);
		parameterInfo=new ParameterInfo();
		parameterInfo.setParamType(new TypeInfo(Object.class.getTypeName()));
		parameterInfo.setParamName(param2);
		list.add(parameterInfo);
		return list;
	}

	@Override
	protected Block buildMethodBody() {
		Block block = ast.newBlock();
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setExpression(ast.newSimpleName(ctx.getDataImplTypes().get(nodeCode).getFullName()));
		methodInvocation.setName(ast.newSimpleName(DataStaticSetValueGenerator.methodName));
		methodInvocation.arguments().add(ast.newThisExpression());
		methodInvocation.arguments().add(ast.newSimpleName(param1));
		methodInvocation.arguments().add(ast.newSimpleName(param2));
		block.statements().add(ast.newExpressionStatement(methodInvocation));
		return block;
	}

//	private void setSwitchExpresssion(SwitchStatement switchStatement) {
//		switchStatement.setExpression(ast.newSimpleName(param1));
//	}
//
//	private void setSwitchCases(SwitchStatement switchStatement) {
//		dataType.stream().forEach(item -> {
//			if (item.getElement() != null) {
//				if(!item.getExtendInfos().containsKey("AssoUdtAtrribute")){
//					buildPropertyCaseStatement(item.getElement(), switchStatement);
//				}
//			}
//		});
//	}
//
//	private void buildPropertyCaseStatement(IGspCommonField field, SwitchStatement switchStatement)
//	{
//		SwitchCase switchCase1=ast.newSwitchCase();
//
//		StringLiteral stringLiteral=ast.newStringLiteral();
//		stringLiteral.setLiteralValue(field.getLabelID());
//		switchCase1.setExpression(stringLiteral);
//		switchStatement.statements().add(switchCase1);
//
//		setSwitchCaseBody(switchStatement,field);
//
//		switchStatement.statements().add(ast.newBreakStatement());
//	}
//
//	private void setSwitchCaseBody(SwitchStatement switchStatement, IGspCommonField field) {
//		if(field.getIsUdt())
//		{
//			UnifiedDataTypeDef udt = parentContext.getProjContext().getCefGenContext().getMetadataContent(field.getUdtPkgName(), field.getUdtID());
//			if(udt instanceof SimpleDataTypeDef) {
//				setUdtSwitchCaseBody(switchStatement, field, (SimpleDataTypeDef)udt);
//				return;
//			}
//		}
//
//		if(field.getObjectType()== GspElementObjectType.Enum){//如果是枚举类 setmj((mjEnum)ValueConvertUtils.toEnum(mjEnum.class,value));
//			setEnumSwitchCaseBody(switchStatement, field);
//		}
//		else
//			setSimpleSwitchCaseBody(switchStatement,field);
//	}
//
//	private void setEnumSwitchCaseBody(SwitchStatement switchStatement, IGspCommonField field)
//	{
//		MethodInvocation methodInvocation=ast.newMethodInvocation();
//		methodInvocation.setName(ast.newSimpleName("set"+field.getLabelID()));
//		TypeInfo info = parentContext.getFieldTypeInfo(nodeCode, field);
//		MethodInvocation castMethodInvocation = ast.newMethodInvocation();
//		castMethodInvocation.setExpression(ast.newSimpleName("ValueConvertUtils"));
//		castMethodInvocation.setName(ast.newSimpleName("convertToEnum"));
//		TypeLiteral typeLiteral = ast.newTypeLiteral();
//		typeLiteral.setType(info.getType(ast,parentContext.getProjContext().getCefGenContext().getCoreGenContext()));
//		castMethodInvocation.arguments().add(typeLiteral);
//		castMethodInvocation.arguments().add(ast.newSimpleName("value"));
//		methodInvocation.arguments().add(castMethodInvocation);
//		ExpressionStatement expressionStatementx=ast.newExpressionStatement(methodInvocation);
//		switchStatement.statements().add(expressionStatementx);
//	}
//
//	private void setSimpleSwitchCaseBody(SwitchStatement switchStatement, IGspCommonField field)
//	{
//		MethodInvocation methodInvocation=ast.newMethodInvocation();
//		methodInvocation.setName(ast.newSimpleName("set"+field.getLabelID()));
//
//		CastExpression castExpression=ast.newCastExpression();
//		TypeInfo info = parentContext.getFieldTypeInfo(nodeCode,field);
//		castExpression.setType(info.getType(ast,parentContext.getProjContext().getCefGenContext().getCoreGenContext()));
//		castExpression.setExpression(ast.newSimpleName(param2));
//		methodInvocation.arguments().add(castExpression);
//		ExpressionStatement expressionStatementx=ast.newExpressionStatement(methodInvocation);
//		switchStatement.statements().add(expressionStatementx);
//	}
//
//	private void setUdtSwitchCaseBody(SwitchStatement switchStatement, IGspCommonField field, SimpleDataTypeDef udt)
//	{
//		IfStatement ifStatement=ast.newIfStatement();
//		setUdtSwitchCaseIfExpression(ifStatement,field, udt);
//		setUdtSwitchCaseIfThen(ifStatement,field);
//		setUdtSwitchCaseIfElse(ifStatement,field);
//
//
//		switchStatement.statements().add(ifStatement);
//	}
//
//	private void setUdtSwitchCaseIfExpression(IfStatement ifStatement, IGspCommonField field, SimpleDataTypeDef udt) {
//
//		InfixExpression infixExpression1=ast.newInfixExpression();
//		infixExpression1.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
//
//		InfixExpression infixExpression =ast.newInfixExpression();
//		infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
//
//		MethodInvocation methodInvocation=ast.newMethodInvocation();
//		methodInvocation.setName(ast.newSimpleName("get"+field.getLabelID()));
//		infixExpression.setLeftOperand(methodInvocation);
//		infixExpression.setRightOperand(ast.newNullLiteral());
//		infixExpression1.setLeftOperand(infixExpression);
//		parentContext.getProjContext().getCefGenContext().getMetadataContent(field.getUdtPkgName(), field.getUdtID());
//		TypeInfo udtItemType = SimpleUdtDataValuePropGenerator.getSimpleUdtDataGenericType(udt);// (Class)ReflectionUtils.getGenericTypesOf(propertyType, ISimpleUdtData.class)[0];//(Class) ((java.lang.reflect.ParameterizedType)propertyType.getGenericInterfaces()[0]).getActualTypeArguments()[0];
//		InstanceofExpression instanceofExpression=ast.newInstanceofExpression();
//		instanceofExpression.setLeftOperand(ast.newSimpleName(param2));
//		instanceofExpression.setRightOperand(SetValueGenerator.getType(ast,udtItemType));
//		//instanceofExpression.setRightOperand(ast.newSimpleType(ast.newName(udtItemType.getTypeName())));
//		infixExpression1.setRightOperand(instanceofExpression);
//		ifStatement.setExpression(infixExpression1);
//	}
//
//	private void setUdtSwitchCaseIfThen(IfStatement ifStatement, IGspCommonField field)
//	{
//		Block block=ast.newBlock();
//		MethodInvocation methodInvocation=ast.newMethodInvocation();
//
//		MethodInvocation methodInvocation1=ast.newMethodInvocation();
//		methodInvocation1.setName(ast.newSimpleName("get"+field.getLabelID()));
//		methodInvocation.setExpression(methodInvocation1);
//
//		methodInvocation.setName(ast.newSimpleName("setValue"));
//		MethodInvocation methodInvocation2=ast.newMethodInvocation();
//		methodInvocation2.setName(ast.newSimpleName("getValue"));
//		ParenthesizedExpression parenthesizedExpression=ast.newParenthesizedExpression();
//
//		CastExpression castExpression=ast.newCastExpression();
//		TypeInfo info= parentContext.getFieldTypeInfo(nodeCode,field);
//		info.setUseFullName(true);
//		castExpression.setType(info.getType(ast));
//		castExpression.setExpression(ast.newSimpleName(param2));
//		parenthesizedExpression.setExpression(castExpression);
//		methodInvocation2.setExpression(parenthesizedExpression);
//		methodInvocation.arguments().add(methodInvocation2);
//
//		ExpressionStatement expressionStatement=ast.newExpressionStatement(methodInvocation);
//		block.statements().add(expressionStatement);
//		ifStatement.setThenStatement(block);
//	}
//
//	private void setUdtSwitchCaseIfElse(IfStatement ifStatement, IGspCommonField field)
//	{
//		Block block =ast.newBlock();
//		MethodInvocation methodInvocation=ast.newMethodInvocation();
//		methodInvocation.setName(ast.newSimpleName(Utils.getSetterMethodName(field.getLabelID())));
//
//		CastExpression castExpression=ast.newCastExpression();
//		TypeInfo info= parentContext.getFieldTypeInfo(nodeCode,field);
//		info.setUseFullName(true);
//		castExpression.setType(info.getType(ast));
//		castExpression.setExpression(ast.newSimpleName(param2));
//		methodInvocation.arguments().add(castExpression);
//
//		ExpressionStatement expressionStatement=ast.newExpressionStatement(methodInvocation);
//		block.statements().add(expressionStatement);
//		ifStatement.setElseStatement(block);
//	}
//
//	private void setSwitchDefaultCase(SwitchStatement switchStatement)
//	{
//		SwitchCase defaultCase = ast.newSwitchCase();
//		defaultCase.setExpression(null);
//		switchStatement.statements().add(defaultCase);
//
//		ThrowStatement throwStatement=ast.newThrowStatement();
//		ClassInstanceCreation classInstanceCreation =ast.newClassInstanceCreation();
//
//		classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//
//
//		InfixExpression infixExpression =ast.newInfixExpression();
//		infixExpression.setOperator(InfixExpression.Operator.PLUS);
//		StringLiteral stringLiteral= ast.newStringLiteral();
//		stringLiteral.setLiteralValue("属性名不存在：");
//		infixExpression.setLeftOperand(stringLiteral);
//		infixExpression.setRightOperand(ast.newSimpleName(param1));
//		classInstanceCreation.arguments().add(infixExpression);
//
//		throwStatement.setExpression(classInstanceCreation);
//		switchStatement.statements().add(throwStatement);
//	}
}
